package com.magic.smartcity.view.home;

import android.app.ActionBar;
import android.app.Application;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;

import androidx.annotation.LongDef;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.magic.smartcity.R;
import com.magic.smartcity.beans.NewsCategoryBean;
import com.magic.smartcity.beans.PressBean;
import com.magic.smartcity.beans.ServiceListBean;
import com.magic.smartcity.beans.TocationBean;
import com.magic.smartcity.databinding.ActivityMainBinding;
import com.magic.smartcity.interfaces.INewsCallBack;
import com.magic.smartcity.interfaces.INewsPresenter;
import com.magic.smartcity.interfaces.ISystemServicesCallBack;
import com.magic.smartcity.interfaces.ISystemServicesPresenter;
import com.magic.smartcity.ioc.MyModule;
import com.magic.smartcity.presenters.NewsPresenter;
import com.magic.smartcity.view.adapters.HomeGuideImageAdapter;
import com.magic.smartcity.view.adapters.HomeHotAdapter;
import com.magic.smartcity.view.adapters.HomeNewsAdapter;
import com.magic.smartcity.view.adapters.HomeNewsCategoryAdapter;
import com.magic.smartcity.view.adapters.HomeServiceAdapter;
import com.youth.banner.Banner;
import com.youth.banner.indicator.BaseIndicator;

import java.util.Comparator;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class HomeFragment extends Fragment implements INewsCallBack, ISystemServicesCallBack {
    private final String TAG = "HomeFragment";
    @BindView(R.id.service_topic)
    GridView mGridView_service_topic;
    @BindView(R.id.hot_topic)
    GridView mGridView_hot_topic;
    @BindView(R.id.news_topic)
    RecyclerView mRecyclerView_news_topic;
    @BindView(R.id.home_guide_image)
    Banner mHome_guide_image;
    @BindView(R.id.news_category_ry)
    RecyclerView news_category;

    private HomeNewsAdapter mNewsAdapter;
    private HomeHotAdapter mHotAdapter;
    private HomeServiceAdapter mServiceAdapter;
    private HomeGuideImageAdapter mHomeGuideImageAdapter;

    @Inject
    INewsPresenter mPresenter;
    @Inject
    ISystemServicesPresenter mServicesPresenter;
    private HomeNewsCategoryAdapter mHomeNewsCategoryAdapter;


    //////////////////////////////////   初始化数据
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mServicesPresenter.registerCallBack(this);
        mPresenter.registerCallBack(this);
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        mServicesPresenter.unRegisterCallBack(this);
        mPresenter.unRegisterCallBack(this);
    }

    //////////////////////////////   初始化数据 -- end

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);
        ButterKnife.bind(this, view);
        initView();
        return view;
    }

    private void initView() {
        //轮播图
        mPresenter.getRotationImages();//获取轮播图数据
        mHomeGuideImageAdapter = new HomeGuideImageAdapter(null);
        mHome_guide_image.setAdapter(mHomeGuideImageAdapter);
        mHome_guide_image.setIndicator(new BaseIndicator(this.getContext()));


        //应用服务
        mServicesPresenter.getAllService();
        mServiceAdapter = new HomeServiceAdapter(1);
        mGridView_service_topic.setAdapter(mServiceAdapter);

        //热门主题
        mHotAdapter = new HomeHotAdapter();
        mGridView_hot_topic.setAdapter(mHotAdapter);

        //新闻专栏
        //新闻分类
        mHomeNewsCategoryAdapter = new HomeNewsCategoryAdapter();
        news_category.setAdapter(mHomeNewsCategoryAdapter);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
        news_category.setLayoutManager(linearLayoutManager);
        mHomeNewsCategoryAdapter.setOnClickItem(position -> {
            mPresenter.getNewList(position);
        });
        //新闻列表
        mPresenter.getNewCategory();//获取新闻分类数据
        mPresenter.getNewList(-1);//获取新闻数据
        mNewsAdapter = new HomeNewsAdapter();
        mRecyclerView_news_topic.setAdapter(mNewsAdapter);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView_news_topic.setLayoutManager(layoutManager);
        mRecyclerView_news_topic.setHasFixedSize(true);

        mPresenter.registerCallBack(this);

    }

    ///////////////////////////回调接口实现


    @Override
    public void NewsLoaded(List<PressBean.RowsBean> pressList) {
        Log.d(TAG, "成功获取了新闻列表的数据:::::" +pressList.size());
        mNewsAdapter.setData(pressList);
    }

    @Override
    public void NewsTocation(List<TocationBean.RowsBean> tocationList) {
        Log.d(TAG, "成功获取了新闻轮播图的数据:::::" + tocationList.size());
        mHomeGuideImageAdapter.setDatas(tocationList);
    }

    @Override
    public void NewsCategoryLoaded(List<NewsCategoryBean.DataBean> dataBeanList) {
        Log.d(TAG, "成功获取了新闻分类的数据:::::" + dataBeanList.size());
        mHomeNewsCategoryAdapter.setData(dataBeanList);
    }

    @Override
    public void error(String str) {

    }

    @Override
    public void AllServiceLoaded(List<ServiceListBean.RowsBean> rowsBean) {
        Log.d(TAG,"成功获取了全部服务应用的数据:::::"+rowsBean.size());
        mServiceAdapter.setData(rowsBean);
    }
}